<!-- 项目设计策划表 -->
<template>
  <div>
    <a-card>
      <title-name title="项目设计策划表" />
      <Form :baseFormInfo="baseFormInfo" :fetching="fetching" :form="form" :memberList="memberList" ref="Form" :queryInfo="queryInfo"></Form>
      <PlanningTable :fetching="fetching" :form="form" :memberList="memberList" :queryInfo="queryInfo"></PlanningTable>
      <OtherForm1 v-show="form.planType == 0" :form="form" ref="OtherForm1" :queryInfo="queryInfo"></OtherForm1>
      <OtherForm2 v-show="form.planType == 1" :form="form" ref="OtherForm2" :queryInfo="queryInfo"></OtherForm2>
    </a-card>
  </div>
</template>

<script>
import Form from './ProjectDesignPlanningForm/Form'
import PlanningTable from './ProjectDesignPlanningForm/PlanningTable'
import OtherForm1 from './ProjectDesignPlanningForm/OtherForm1'
import OtherForm2 from './ProjectDesignPlanningForm/OtherForm2'
import { getOrganizeUserListQuick } from '@/api/commons'

import { getPlanDesignById, updateByPrimaryKeySelectiveplanDesign } from '@/api/projectManagementPlanning/projectPlanningTaskList'
export default {
  name: 'ProjectDesignPlanningForm',
  props: {
    baseFormInfo: {
      type: Object,
      default: () => {}
    },
    spinning: Boolean
  },
  data() {
    return {
      fetching: false,
      form: {},
      memberList: [], // 接收人
      queryInfo: {
        id: '',
        pageType: ''
      }
    }
  },

  components: { Form, PlanningTable, OtherForm1, OtherForm2 },

  computed: {},

  watch: {
    baseFormInfo: {
      handler(val) {
        if (val.projectId) {
          this.getImplees()
        }
      },
      immediate: true
    }
  },

  created() {
    const { id, pageType, version = 0 } = this.$route.query
    /**
     * version 不存在 就是正常入口进来的。
     * 否则 就是在V1等弹窗进来的
     */
    this.queryInfo.id = id
    this.queryInfo.pageType = pageType
    this.getPlanDesignById()
  },

  mounted() {},

  methods: {
    // 提交草稿
    ProjectDesignPlanningFormSaveDrafts() {
      this.$emit('update:spinning', true)
      const dataClone = JSON.parse(JSON.stringify(this.form))
      if (this.form.planType == 0) {
        Object.assign(dataClone, this.$refs.OtherForm1.otherform1, { statusEnum: 'DRAFT', changed: this.queryInfo.pageType === 'change' ? true : false })
      } else {
        Object.assign(dataClone, this.$refs.OtherForm2.otherform2, { statusEnum: 'DRAFT', changed: this.queryInfo.pageType === 'change' ? true : false })
      }
      updateByPrimaryKeySelectiveplanDesign(dataClone)
        .then((res) => {
          this.$emit('update:spinning', false)
          this.$message.success('保存成功')
        })
        .catch(() => {
          this.$emit('update:spinning', false)
        })
    },
    // 提交
    ProjectDesignPlanningFormSubmit(formBase) {
      let pass1 = true
      let pass2 = true
      let pass3 = true
      this.$refs.Form.$refs.planningFormBase.validate((valid) => {
        if (valid) {
          pass1 = true
        } else {
          pass1 = false
        }
      })
      if (this.form.planType == 0) {
        this.$refs.OtherForm1.$refs.OtherForm1.validate((valid) => {
          if (valid) {
            pass2 = true
          } else {
            pass2 = false
          }
        })
      } else {
        this.$refs.OtherForm2.$refs.OtherForm2.validate((valid) => {
          if (valid) {
            pass2 = true
          } else {
            pass2 = false
          }
        })
      }

      const emypArr = this.form.basisList.filter((v) => {
        if (v.content == '' || v.content == null || v.content == undefined || v.content.replace(/\s*/g, '') == '') {
          return v
        }
        if (v.finishjob == '' || v.finishjob == null || v.finishjob == undefined || v.finishjob.replace(/\s*/g, '') == '') {
          return v
        }
        if (v.finishTime == '' || v.finishTime == null || v.finishTime == undefined || v.finishTime.replace(/\s*/g, '') == '') {
          return v
        }
        if (v.contactId == '' || v.contactId == null || v.contactId == undefined || v.contactId.replace(/\s*/g, '') == '') {
          return v
        }
      })
      if (emypArr && emypArr.length) {
        pass3 = false
      }
      if (!pass1 || !pass2 || !pass3) {
        this.$warning({
          title: '提示',
          content: '请将数据填写完整',
          okText: '确定'
        })
        return
      }
      this.$confirm({
        title: '提交',
        content: '请确认是否要提交？',
        onOk: async () => {
          this.$emit('update:spinning', true)
          const dataClone = JSON.parse(JSON.stringify(this.form))
          if (this.form.planType == 0) {
            Object.assign(dataClone, this.$refs.OtherForm1.otherform1, { statusEnum: 'SUBMITTED', changed: this.queryInfo.pageType === 'change' ? true : false })
          } else {
            Object.assign(dataClone, this.$refs.OtherForm2.otherform2, { statusEnum: 'SUBMITTED', changed: this.queryInfo.pageType === 'change' ? true : false })
          }
          updateByPrimaryKeySelectiveplanDesign(dataClone)
            .then((res) => {
              this.$emit('update:spinning', false)
              this.$message.success('保存成功')
              this.$close()
            })
            .catch(() => {
              this.$emit('update:spinning', false)
            })
        }
      })
    },
    getGuid() {
      return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
        var r = (Math.random() * 16) | 0
        var v = c === 'x' ? r : (r & 0x3) | 0x8
        return v.toString(16)
      })
    },
    // 获取项目设计策划表（09表）信息
    getPlanDesignById() {
      this.$emit('update:spinning', true)
      const query = {
        planTaskDetailId: this.queryInfo.id,
        latestData: true
      }
      if (this.queryInfo.version) {
        query.latestData = false
        query.version = this.queryInfo.version
      }
      getPlanDesignById(query)
        .then((res) => {
          this.$emit('update:spinning', false)
          if (res.data.designStage == null) {
            res.data.designStage = undefined
          }
          if (res.data.planType == null) {
            res.data.planType = 0
          }
          res.data.basisList.map((v) => {
            if (v.contactId == null) {
              v.contactId = undefined
            }
          })
          if (res.data.responsibleId == null) {
            res.data.responsibleId = undefined
          }

          res.data.basisList.map((v) => {
            v.guid = this.getGuid()
          })
          this.form = res.data
        })
        .catch(() => {
          this.$emit('update:spinning', false)
        })
    },
    // 获取接收人
    getImplees() {
      this.fetching = true
      getOrganizeUserListQuick({ projectId: this.baseFormInfo.projectId }).then((res) => {
        const obj = {}
        // 去掉重复以及employeeId为空的数据
        res.data = res.data.reduce(function (item, next) {
          obj[next.employeeId] || !next.employeeId ? '' : (obj[next.employeeId] = true && item.push(next))
          return item
        }, [])
        this.memberList = res.data
        this.fetching = false
      })
    }
  }
}
</script>
<style lang='less' scoped>
</style>
